package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.zhentao.mapper.ApUserMapper;
import com.zhentao.mapper.ApUserSocialMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.social.pojo.ApUserLike;
import com.zhentao.model.social.pojo.ApUserSocial;
import com.zhentao.model.social.vo.UserLikeVo;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.service.ApUserLikeService;
import com.zhentao.mapper.ApUserLikeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ap_user_like】的数据库操作Service实现
* @createDate 2024-07-05 14:30:24
*/
@Service
public class ApUserLikeServiceImpl extends ServiceImpl<ApUserLikeMapper, ApUserLike>
    implements ApUserLikeService{
    @Resource
    ApUserLikeMapper apUserLikeMapper;
    @Resource
    ApUserSocialMapper apUserSocialMapper;

    @Override
    public ResponseResult addLike(ApUserLike apUserLike) {
        QueryWrapper<ApUserSocial> wrapper=new QueryWrapper<>();
        //根据点赞表中的动态id从动态表中获取动态对象
        wrapper.lambda().eq(ApUserSocial::getId,apUserLike.getSocialId());
        ApUserSocial apUserSocial = apUserSocialMapper.selectOne(wrapper);
        //设置中间表中的动她
        apUserLike.setToUserId(apUserSocial.getUserId());
        UserLikeVo LikeUser=apUserLikeMapper.getUserByUid(apUserLike.getLikeUserId());
        apUserLike.setNickName(LikeUser.getNickName());
        apUserLike.setAvatarUrl(LikeUser.getAvatarUrl());
        apUserLike.setCreatedTime(new Date());
        apUserLikeMapper.insert(apUserLike);
        apUserSocial.setLikeNum(apUserSocial.getLikeNum()+1);
        apUserSocialMapper.updateById(apUserSocial);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getList(Integer id) {
        List<UserLikeVo> likeUsers =apUserLikeMapper.getUserBySid(id);
        return ResponseResult.okResult(likeUsers);

    }

    @Override
    public ResponseResult delLike(ApUserLike apUserLike) {
        QueryWrapper<ApUserLike> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApUserLike::getLikeUserId,apUserLike.getLikeUserId());
        wrapper.lambda().eq(ApUserLike::getSocialId,apUserLike.getSocialId());
        ApUserLike apUserLike1 = apUserLikeMapper.selectOne(wrapper);
        apUserLikeMapper.deleteById(apUserLike1.getId());
        QueryWrapper<ApUserSocial> queryWrapper=new QueryWrapper<>();
        //根据点赞表中的动态id从动态表中获取动态对象
        queryWrapper.lambda().eq(ApUserSocial::getId,apUserLike.getSocialId());
        ApUserSocial apUserSocial = apUserSocialMapper.selectOne(queryWrapper);
        apUserSocial.setLikeNum(apUserSocial.getLikeNum()-1);
        apUserSocialMapper.updateById(apUserSocial);
        return  ResponseResult.okResult("取消成功");
    }
}




